layui.use('layer', function(){
    var layer = layui.layer;
    $('.go_btn').click(function() {
        var token=$('#token').val();
        var username = $(".username").val();
        var patrn1 = /^[a-zA-Z0-9]{1}([a-zA-Z0-9]|[._,+-@#$%^&*()~`]){2,20}$/;
        if(!patrn1.test(username)) {

            $(".username").addClass("error_red");
            return false;
        }else{
            $(".username").removeClass("error_red");
        }
        var pwd = $(".password").val();
        if(!patrn1.test(pwd)) {
            $(".password").addClass("error_red");
            return false
        }else{
            $(".password").removeClass("error_red");
        }
        var agin_pass=$(".aginpass").val();
        if(agin_pass!=pwd){
            $(".aginpass").addClass("error_red");
            layer.msg("两次密码不一致！")
            return false;
        }else{
            $(".aginpass").removeClass("error_red");
        }
        var phone = $(".iphone").val();
        var patrn2 = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if(!patrn2.test(phone)) {
            $(".iphone").addClass("error_red");
            return false;
        }else{
            $(".iphone").removeClass("error_red");
        }
        var regs = /^.{4}$/
        var verification_numb = $(".verification_numb").val();
        if(!regs.test(verification_numb)) {
            $(".verification_numb").addClass("error_red");
            layer.msg("非法的图形验证码")
            return false
        }else{
            $(".verification_numb").removeClass("error_red");
        }
        var reg = /^\d{4}$/;
        var code = $(".code").val();
        if(!reg.test(code)) {
            $(".code").addClass("error_red");
            layer.msg("非法的验证码格式");
            return false
        }else{
            $(".code").removeClass("error_red");
        }

        var data = {
            'username':username,
            'password':pwd,
            'mobile':phone,
            'code':code,
            '_token':token,
            'validateCode':verification_numb
        };

        // url = $('#url').val();
        $.ajax({
            url: '/agent/register',
            type: "POST",
            data: data,
            beforeSend:function(){
                $(".go_btn").attr("disabled",true);
            },
            success: function(ret){
                if(ret.error_code != 0)
                {
                    layui.layer.msg(ret.error_message, {
                        icon: 6,
                        time: 2000
                    });
                    $(".go_btn").attr("disabled",false);
                }else{
                    layer.open({
                        type: 1,
                        title: '恭喜您完成注册',
                        skin: 'layui-layer-demo',
                        btn: ['确定'],
                        anim: 2,
                        shadeClose: true,
                        content: '<p class="pp1">点击确定两秒自动跳转至 > 代理商后台系统</p>',
                        yes: function(index){
                            layer.close(index);
                            $(".even_stop").show();
                            window.setTimeout("window.location.href='http://admin.niumoip.com'",2000);
                        },
                        cancel: function(){
                        }
                    });
                    //
                    // self.location.href='/personal/center';
                }
            }
        });



    });
//短信验证码
    var wait=60;
    var isclick = true;
    function time(o) {
        wait--;
        o.html(wait+'秒后获取验证码');
        if(wait == 0){
            isclick = true;
            o.html('重新发送验证码');
            wait=60;
            return false;
        }
        isclick = false;
        setTimeout(function() {
                time(o);
            },
            1000)
    }

    $('#getCode').click(function (){
        var token=$('#token').val();
        var username = $(".username").val();
        var _this=$(this);
        var pwd = $(".password").val();
        var phone = $(".iphone").val();
        var verification_numb = $(".verification_numb").val();
        var patrn2 = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if(username !='' && pwd !='' &&pwd!='' && verification_numb!=''){
        }else{
            layer.msg("内容不能为空");
            return false;
        }
        if(isclick){
            if(!patrn2.test(phone)) {
                $(".iphone").addClass("error_red");
                return false;
            }else{
                $(".iphone").removeClass("error_red");
            }
            var data = {
                'validateCode':verification_numb,
                'mobile':phone,
                'action':'invite_agent',
                '_token':token
            };

            $.ajax({
                url: '/code',
                type: "post",
                data: data,
                success: function(ret){
                    if(ret.code != 0)
                    {
                        layui.layer.msg(ret.msg);
                    }else{
                        time(_this);
                    }
                }
            });


        }
    })


});